package com.smartlink.util;

import java.io.File;

import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

public class EmailUtil {
	private JavaMailSender mailSender;
	private Log log = LogFactory.getLog(EmailUtil.class);

	public JavaMailSender getMailSender() {
		return mailSender;
	}

	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	/**
	 * 发送报警邮件
	 * 
	 * @param email
	 * @param classromm
	 * @param errorCode
	 * @param images
	 * @throws Exception
	 */
	public void sendEmail(String email, String classromm, String errorCode, File[] images) throws Exception {
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
		helper.setFrom("smartlinke@163.com");
		helper.setTo(email);
		helper.setSubject("smartlink智能教室报警");
		String text = getText(classromm, errorCode);
		helper.setText("<html><body>" + text + "</body></html>", true);
		if (images != null && images.length > 0) {

			for (int i = 0; i < images.length; i++) {
				helper.addAttachment(System.currentTimeMillis() + ".jpg", images[i]);
			}
		}
		mailSender.send(message);
		log.info("发送报警");

	}

	/**
	 * 发送管理员忘记密码验证码
	 * 
	 * @param email
	 * @param identifiCode
	 * @throws Exception
	 */
	public void sendEmail(String email, int identifiCode) throws Exception {
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
		helper.setFrom("smartlinke@163.com");
		helper.setTo(email);
		helper.setSubject("smartlink验证码");
		helper.setText("您的验证码是：" + identifiCode + "!(10分钟内有效)");
		mailSender.send(message);
		log.info("发送验证码");
	}

	private String getText(String classromm, String errorCode) {
		int people = Integer.parseInt(errorCode.substring(0, 3));
		int fire = Integer.parseInt(errorCode.substring(3));
		StringBuffer sb = new StringBuffer();//
		sb.append("If you received this message, the system detected " + classromm + " exception:</br>");
		sb.append("<ul>");
		switch (people) {
		case 110:
			sb.append("<li><p style=\"color:red\">Burglar alarm: doors and windows were opened</p></li>");
			sb.append("<li><p style=\"color:red\">Burglar alarm:Indoor activities</p></li>");
			break;
		case 111:
			sb.append("<li><p style=\"color:red\">Burglar alarm:Indoor activities</p></li>");
			break;
		case 112:
			sb.append("<li><p style=\"color:red\">Burglar alarm: doors and windows were opened</p></li>");
			break;
		}

		switch (fire) {
		case 119:
			sb.append("<li><p style=\"color:red\">Fire alarm:notice open flame</p></li>");
			sb.append("<li><p style=\"color:red\">Fire alarm:notice combustible gas</p></li>");
			break;
		case 118:
			sb.append("<li><p style=\"color:red\">Fire alarm:notice open flame</p></li>");
			break;
		case 117:
			sb.append("<li><p style=\"color:red\">Fire alarm:notice combustible gas</p></li>");
			break;
		}
		sb.append("</ul>");
		sb.append("Photo in attachment,please check!");
		sb.append("<h2>Please deal with in time!</h2>");
		return sb.toString();
	}

}
